import { defHttp } from '/@/utils/http/axios';

import { ErrorMessageMode } from '/#/axios';
import {
  FormTemplateListModel,
  FormTemplateListParams,
  FormTemplateListResultModel,
  FormTemplateModel,
} from './model';

enum Api {
  FormTemplate = '/form/template',
  List = '/form/template/list',
  Page = '/form/template/page',
  Enabled = '/form/template/enabled-page',
  Info = '/form/template/info',
  Status = '/form/template/status',
  DataFirst = '/form/template/data-first',
  CodeFirst = '/form/template/code-first',
  History = '/form/history/list',
  HistoryInfo = '/form/history/info',
}

/**
 * @description: 查询所有模板 （不分页）
 */
export async function getFormTemplateList(
  params?: FormTemplateListParams,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.get<FormTemplateListModel[]>(
    {
      url: Api.List,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description:  查询所有模板 （分页）
 */
export async function getFormTemplatePage(params: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<FormTemplateListResultModel>(
    {
      url: Api.Enabled,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description:  查询所有启用的模板 （分页）
 */
export async function getFormTemplateEnabledPage(
  params: Recordable,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.get<FormTemplateListResultModel>(
    {
      url: Api.Page,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 删除模板（批量删除）
 */
export async function deleteFormTemplate(ids: string[], mode: ErrorMessageMode = 'modal') {
  return defHttp.delete<number>(
    {
      url: Api.FormTemplate,
      data: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 获取模板详情信息
 */
export async function getFormTemplate(id: string, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<FormTemplateModel>(
    {
      url: Api.Info,
      params: { id },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 更新模板状态
 */
export async function updateFormTemplateStatus(
  id: string,
  status: number,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.put<number>(
    {
      url: Api.Status,
      data: {
        id,
        enabledMark: status,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 更新模板（数据优先）
 */
export async function updateDataFirstFormTemplate(
  formTemplate: Recordable,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.put<number>(
    {
      url: Api.DataFirst,
      data: formTemplate,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 新增模板（数据优先）
 */
export async function addDataFirstFormTemplate(
  formTemplate: Recordable,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.post<number>(
    {
      url: Api.DataFirst,
      data: formTemplate,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 查询自定义表单历史记录
 */
export async function getFormHistory(formId: string, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<any>(
    {
      url: Api.History,
      params: { formId },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 查询自定义表单历史记录预览
 */
export async function getFormHistoryInfo(id: string, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<FormTemplateModel>(
    {
      url: Api.HistoryInfo,
      params: { id },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 新增模板（界面优先、简易模板）
 */
export async function addCodeFirstFormTemplate(
  formTemplate: Recordable,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.post<number>(
    {
      url: Api.CodeFirst,
      data: formTemplate,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 更新模板（界面优先、简易模板）
 */
export async function updateCodeFirstFormTemplate(
  formTemplate: Recordable,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.put<number>(
    {
      url: Api.CodeFirst,
      data: formTemplate,
    },
    {
      errorMessageMode: mode,
    },
  );
}
